<template>
  <div>
    <el-dialog
      :visible.sync="dialogVisible"
      :before-close="handleCancel"
      :show-close="true"
      :append-to-body="true"
      :close-on-click-modal="false"
      :class="{'global-mobile-dialog': showMobilePage}"
      class="deal-dialog"
      width="400px"
    >
      <Title title="设置无需核销金额" font-size="16px" style="margin-bottom:16px;"/>
      <el-form>
        <el-form-item label="可设置金额:">
          <PriceColumn :real-val="maxAmount" :price="maxAmount / 100" />
        </el-form-item>
      </el-form>
      <el-input
        v-model="noWriteOffAmount"
        size="small"
        type="number"
        class="width240"
        placeholder="请输入金额"
        oninput="if(value>999999999)value=999999999"
        @input="numInputChange"/>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" type="primary" @click="handleSubmit">确认</el-button>
        <el-button type="info" class="cancel-btn-style" size="small" @click="handleCancel">取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Title from '@/components/Title';
import ElMessage from '@/utils/message';
import { setNoWriteOff } from '@/api/finance';
import { mapState } from 'vuex';
import math from '@/utils/math';

export default {
  components: { Title },
  model: {
    prop: 'paramsObj',
    event: 'change'
  },
  data() {
    return {
      noWriteOffAmount: 0,
      dialogVisible: false,
      maxAmount: 0,
      writeOffAmount: 0
    };
  },
  computed: {
    ...mapState({
      'showMobilePage': state => state.app.showMobilePage
    })
  },
  methods: {
    openDialog(noWriteOffAmount, data) {
      this.noWriteOffAmount = noWriteOffAmount / 100;
      this.writeOffAmount = data;
      this.maxAmount = (data || 0) + (noWriteOffAmount || 0);
      this.dialogVisible = true;
    },
    // 限制无需核销金额
    numInputChange(value) {
      const temp = Number(value) * 100;
      // 可核销金额为正，输入要为正
      if (this.writeOffAmount > 0 && value < 0) {
        this.noWriteOffAmount = 0;
        ElMessage.warning('可设置金额为正，无需核销金额也要为正数！');
        return;
      } else if (this.writeOffAmount < 0 && value > 0) {
        // 可核销金额为负，输入要为负
        this.noWriteOffAmount = 0;
        ElMessage.warning('可设置金额为负，无需核销金额也要为负数！');
        return;
      }
      if (temp * temp > this.maxAmount * this.maxAmount) {
        this.noWriteOffAmount = this.maxAmount / 100;
        ElMessage.warning('|无需核销金额| 不能大于 |可设置金额|！');
        return;
      }
      // let str = value.replace(/[^\d.]/g, ''); // 只允许数字
      let str = value.replace(/\.{2,}/g, '.'); // 连续两个小数点 replace为.
      str = str.replace(/^\./g, '0.'); // 已点为开头 replace为 0.
      str = str.replace(/^(\d*)\.(\d*)\./g, '$1.$2');// 不连续点点 删掉
      str = str.replace(/^0[^\.]+/g, '0'); // 连续多个0 replace为1个0
      str = str.replace(/^(\d+)\.(\d\d).*$/, '$1.$2'); // 只允许保留小数点后3位
      this.noWriteOffAmount = str;
    },
    closeDialog() {
      this.dialogVisible = false;
    },
    handleSubmit() {
      const amount = math.multiply(this.noWriteOffAmount, 100);
      setNoWriteOff(this.$route.params.chargeOffId, { noWriteOffAmount: amount }).then(res => {
        if (res.code === 200) {
          ElMessage.success('操作成功！');
          this.closeDialog();
          this.$emit('update');
        }
      }).catch(err => {
        console.log(err);
      });
    },
    handleCancel() {
      this.dialogVisible = false;
      this.$emit('closeDealDialog');
    }
  }
};
</script>

<style lang="scss" scoped>
.deal-dialog{
  border-radius: 10px;
  ::v-deep {
    .el-dialog{
      border-radius: 10px;
    }
    .el-dialog__header{
      padding: 0;
    }
    .el-dialog__body{
      padding: 16px 24px 0;
    }
    .dialog-footer{
      display: flex;
      justify-content: center;
    }
    .el-form-item{
      margin-bottom: 14px;
      .width240{
        width: 240px;
      }
      .el-form-item__error{
        padding: 0;
      }
    }
  }
}
</style>
